package tictactoeonline;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

 

/**
 * Das Fenster fuer den bereits laufenden Server.
 * @author Daniel Mueller, Phillip Hertel, Jonas Hauff
 */
public class laufenderServer extends javax.swing.JFrame {

    private Spielserver server;
    /**
     * Creates new form laufenderServer
     */
    public laufenderServer() {
        initComponents();
    }
    /**
     * Server wird uebergeben.
     * @param pServer 
     */
    public void newServer(Spielserver pServer)
    {
        server = pServer;
    }
    /**
     * Veraendert das Label fuer den Port des Servers nach dem uebergebenen Port.
     * @param pPort 
     */
    public void setLabelPort(String pPort)
    {
        LabelServerPort.setText(pPort);
    }
    /**
     * Zeigt die uebergebene Nachricht in dem Textbereich an. 
     * @param pMessage 
     */
    public void protokoll(String pMessage)
    {
        int a = 0;
        a = pMessage.indexOf(":");
        if(a== -1)
        {
            jTextArea.setText(jTextArea.getText()+"\n"+pMessage);
        }
        else
        {
            String pName = pMessage.substring(0,a);
            if(pName.equals("Server"))
            {
                jTextArea.setText(jTextArea.getText()+"\n"+">>Server: "+ pMessage.substring(a+1, pMessage.length()));
            }
            else
            {
                jTextArea.setText(jTextArea.getText()+"\n"+"<<"+pMessage);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelServerIP = new javax.swing.JLabel();
        LabelServerIP = new javax.swing.JLabel();
        jLabelServerPort = new javax.swing.JLabel();
        LabelServerPort = new javax.swing.JLabel();
        jLabelServer = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea = new javax.swing.JTextArea();
        jLabelServerStop = new javax.swing.JLabel();
        jButtonServerStop = new javax.swing.JButton();
        jTextFieldTextFeld1 = new javax.swing.JTextField();
        jButtonAnAlle = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelServerIP.setText("Server IP:");

        LabelServerIP.setText("Localhost");

        jLabelServerPort.setText("Server Port:");

        LabelServerPort.setText("2525");

        jLabelServer.setText("Server 2000");

        jTextArea.setColumns(20);
        jTextArea.setRows(5);
        jTextArea.setFocusable(false);
        jScrollPane1.setViewportView(jTextArea);

        jLabelServerStop.setText("Server stoppen:");

        jButtonServerStop.setText("Stop");

        jTextFieldTextFeld1.setText("Text ....");

        jButtonAnAlle.setText("An alle Senden");
        jButtonAnAlle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAnAlleActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(159, 159, 159)
                                .addComponent(jLabelServer))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelServerPort)
                                    .addComponent(jLabelServerIP))
                                .addGap(38, 38, 38)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(LabelServerPort)
                                    .addComponent(LabelServerIP))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelServerStop)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonServerStop)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextFieldTextFeld1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonAnAlle, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabelServer)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelServerIP)
                    .addComponent(LabelServerIP))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelServerPort)
                    .addComponent(LabelServerPort))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelServerStop)
                    .addComponent(jButtonServerStop))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldTextFeld1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonAnAlle))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * Die Nachricht wird an alle Clients gesendet.(zurzeit wird diese bei den Clients jedoch noch nicht angezeigt)
     * @param evt 
     */
    private void jButtonAnAlleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAnAlleActionPerformed
    server.sendToAll("Server: " + jTextFieldTextFeld1.getText());
    this.protokoll("Server: "+jTextFieldTextFeld1.getText());
    }//GEN-LAST:event_jButtonAnAlleActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(laufenderServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(laufenderServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(laufenderServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(laufenderServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new laufenderServer().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LabelServerIP;
    private javax.swing.JLabel LabelServerPort;
    private javax.swing.JButton jButtonAnAlle;
    private javax.swing.JButton jButtonServerStop;
    private javax.swing.JLabel jLabelServer;
    private javax.swing.JLabel jLabelServerIP;
    private javax.swing.JLabel jLabelServerPort;
    private javax.swing.JLabel jLabelServerStop;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea;
    private javax.swing.JTextField jTextFieldTextFeld1;
    // End of variables declaration//GEN-END:variables
}
